package org.phalanx.client.mapeditor.dialogs;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import org.phalanx.client.PhalanxClient;
import org.phalanx.client.UI.DialogManager;
import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.swing.PhalanxButtonFactory;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.mapeditor.PhalanxMapEditorPanel;
import org.phalanx.client.mapeditor.PhalanxMapMetadataPanel;
import org.phalanx.client.mapeditor.PhalanxMapNameInfoPanel;
import org.phalanx.client.mapeditor.PhalanxMapSizeSetPanel;
import org.phalanx.client.mapeditor.PhalanxMapTileSetViewer;
import org.phalanx.map.MapTile;
import org.phalanx.map.PhalanxMap;

public class PhalanxNewMapDialog extends JFrame implements pButtonListener, WindowListener {
	private static final long serialVersionUID = 2848387173029417003L;
	public static final String NAME = "NEW_MAP_DIALOG";
	private PhalanxMapEditorPanel masterPanel;
	private PhalanxMapMetadataPanel metaDataPanel;
	private PhalanxMapNameInfoPanel nameInfoPanel;
	private PhalanxMapSizeSetPanel setSizePanel;
	private PhalanxMapTileSetViewer tileSetPanel;
	private PhalanxIconButton okButton;
	private PhalanxIconButton cancelButton;
	private JPanel contextPanel;
	
	public PhalanxNewMapDialog(PhalanxMapEditorPanel parentPanel) {
		this.masterPanel = parentPanel;
		initializeFrame();
		createComponents();
		initializePanels();
		createNameInfoGrid();
		createSizePanelGrid();
		createTilePanelGrid();
		createMetadataPanelGrid();
		createOkCancelPanelGrid();
		
		//this.add(contextPanel);
	}
	
	private void createComponents() {
		okButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_OK_BUTTON);
		cancelButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_CANCEL_BUTTON);
		
		okButton.addButtonListener(this);
		cancelButton.addButtonListener(this);
	}
	
	private void initializeFrame() {
		this.setBackground(Color.black);
		this.setSize(624, 816);
		this.setResizable(false);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		this.addWindowListener(this);
	      
		this.setTitle("New Map");
		
		JFrame masterFrame = masterPanel.getMasterFrame();
		int pX = ((masterFrame.getWidth() + masterFrame.getLocation().x) + 
				masterFrame.getLocation().x) / 5;
		int pY = ((masterFrame.getHeight() + masterFrame.getLocation().y) + 
				masterFrame.getLocation().y) / 5;
		
		this.setLocation(pX, pY);
		contextPanel = new JPanel();
		contextPanel.setLayout(new GridBagLayout());
		contextPanel.setBackground(Color.black);
		
		this.setContentPane(contextPanel);	
	}
	
	private void initializePanels() {
		metaDataPanel = new PhalanxMapMetadataPanel();
		nameInfoPanel = new PhalanxMapNameInfoPanel();
		setSizePanel = new PhalanxMapSizeSetPanel();
		tileSetPanel = new PhalanxMapTileSetViewer(masterPanel);
		tileSetPanel.setCurrentMapTileSet(PhalanxClient.getClientInstance().getDefaultTileSet());
		
		TitledBorder b = BorderFactory.createTitledBorder("Default Tile");
		b.setTitleColor(Color.white);
		tileSetPanel.setBorder(b);
		
		b = BorderFactory.createTitledBorder("Meta Info");
		b.setTitleColor(Color.white);
		metaDataPanel.setBorder(b);
		
		b = BorderFactory.createTitledBorder("Map Info");
		b.setTitleColor(Color.white);
		nameInfoPanel.setBorder(b);
	}
	
	private void createNameInfoGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.fill = GridBagConstraints.BOTH;
		c.weighty = 0.5;
		
		contextPanel.add(nameInfoPanel, c);
	}
	
	private void createSizePanelGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		
		setSizePanel.setWidthField(20);
		setSizePanel.setLengthField(20);
		
		contextPanel.add(setSizePanel, c);
	}
	
	private void createTilePanelGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		
		contextPanel.add(tileSetPanel, c);
	}
	
	private void createMetadataPanelGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		c.fill = GridBagConstraints.BOTH;
		c.weighty = 0.5;
		
		contextPanel.add(metaDataPanel, c);
	}
	
	private void createOkCancelPanelGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		c.fill = GridBagConstraints.HORIZONTAL;
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(Color.black);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		
		contextPanel.add(buttonPanel, c);
	}

	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent().equals(okButton)) {
			PhalanxMap newMap = makeNewMap();
			
			if (newMap != null) {
				masterPanel.createNewMap(makeNewMap());
				masterPanel.rehashTileSetViewer();
				masterPanel.getDisplayContainer().getMapViewObject().setCoordinates(0, 0);
				closeWindow();
			}
		}
		else if (p.getComponent().equals(cancelButton)) {
			closeWindow();
		}
	}
	
	private void closeWindow() {
		DialogManager d = DialogManager.getInstance();
		
		d.removeDialog(NAME);
		
		this.dispose();
	}
	
	private PhalanxMap makeNewMap() {
		PhalanxMap newMap = new PhalanxMap();
		String getAuthorName = metaDataPanel.getAuthorName().getText();
		String getAuthorEmail = metaDataPanel.getAuthorEmail().getText();
		String getAuthorCountry = metaDataPanel.getAuthorCountry().getText();
		String getAuthorURL = metaDataPanel.getAuthorURL().getText();
		
		String mapName = nameInfoPanel.getMapName().getText();
		String mapVersion = nameInfoPanel.getMapVersion().getText();
		
		Integer mapWidth = Integer.valueOf(setSizePanel.getWidthField().getText());
		Integer mapLength = Integer.valueOf(setSizePanel.getLengthField().getText());
		
		MapTile defaultTile = tileSetPanel.getSelectedMapTile();
		
		newMap.setMapTileSet(PhalanxClient.getClientInstance().getDefaultTileSet());
		newMap.setMapDimensions(mapLength, mapWidth);
		newMap.setDefaultMapTile(defaultTile, true);
		newMap.setAuthorName(getAuthorName);
		newMap.setAuthorEmail(getAuthorEmail);
		newMap.setAuthorCountry(getAuthorCountry);
		newMap.setAuthorURL(getAuthorURL);
		
		newMap.setMapName(mapName);
		newMap.setMapVersion(mapVersion);
		
		
		if (!validateMap(newMap)) {
			newMap = null;
		}
		
		return newMap;
	}
	
	private boolean validateMap(PhalanxMap newMap) {
		return newMap.getDefaultTile() != null && newMap.getMapWidth() != null 
		&& newMap.getMapLength() != null;
	}

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent arg0) {
		
	}

	public void windowClosing(WindowEvent arg0) {
		this.closeWindow();
	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
